﻿using Basic.FormDAL;
using Basic.FormModel.DB;
using Basic.FormRemoteModel.PhysicalGroup.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Basic.FormCollect.lmpl
{
    internal class PhysicalFormGroupCollect : IPhysicalFormGroupCollect
    {
        private readonly IPhysicalFormGroupDAL _BasicDAL;

        public PhysicalFormGroupCollect ( IPhysicalFormGroupDAL basicDAL )
        {
            this._BasicDAL = basicDAL;
        }

        public long Add ( PhysicalGroupSet obj )
        {
            if ( this._BasicDAL.IsExists(c => c.GroupName == obj.GroupName) )
            {
                throw new ErrorException("form.physical.group.name.repeat");
            }
            else if ( this._BasicDAL.IsExists(c => c.SysTypeVal == obj.SysTypeVal) )
            {
                throw new ErrorException("form.physical.sys.type.repeat");
            }
            return this._BasicDAL.Add(obj);
        }
        public bool Set ( DBPhysicalFormGroup source, PhysicalGroupSet obj )
        {
            if ( source.IsEnable )
            {
                throw new ErrorException("form.physical.group.enable");
            }
            else if ( source.GroupName != obj.GroupName && this._BasicDAL.IsExists(c => c.GroupName == obj.GroupName) )
            {
                throw new ErrorException("form.physical.group.name.repeat");
            }
            else if ( source.SysTypeVal != obj.SysTypeVal && this._BasicDAL.IsExists(c => c.SysTypeVal == obj.SysTypeVal) )
            {
                throw new ErrorException("form.physical.sys.type.repeat");
            }
            return this._BasicDAL.Update(source, obj);
        }
        public Result[] Query<Result> ( PhysicalGroupQuery query, IBasicPage paging, out int count ) where Result : class
        {
            return this._BasicDAL.Query<Result>(query, paging, out count);
        }
        public void Delete ( DBPhysicalFormGroup source )
        {
            if ( source.IsEnable )
            {
                throw new ErrorException("form.physical.group.enable");
            }
            this._BasicDAL.Delete(source.Id);
        }

        public Result[] GetEnables<Result> () where Result : class, new()
        {
            return this._BasicDAL.Gets<Result>(a => a.IsEnable);
        }

        public DBPhysicalFormGroup Get ( long id )
        {
            return this._BasicDAL.Get(id);
        }

        public Result Get<Result> ( long id ) where Result : class, new()
        {
            return this._BasicDAL.Get<Result>(id);
        }

        public void SetIsEnable ( DBPhysicalFormGroup source, bool isEnable )
        {
            if ( source.IsEnable == isEnable )
            {
                return;
            }
            this._BasicDAL.SetIsEnable(source.Id, isEnable);
        }
    }
}
